package com.xl.rentkeeper.act.jiedan;

import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.RoomInfo;
import com.xl.fuse.protos.viewroom.RoomStatus;
import com.xl.fuse.protos.viewroom.ViewOrderStatus;
import com.xl.rentkeeper.App;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.BaseAct;
import com.xl.rentkeeper.act.MainActivity;
import com.xl.rentkeeper.act.jilu.NotSignOrderActivity;
import com.xl.rentkeeper.act.jilu.NotViewOrderActivity;
import com.xl.rentkeeper.act.jilu.SignOrderActivity;
import com.xl.rentkeeper.act.kanfang.AffirmOrderRoomAct;
import com.xl.rentkeeper.act.kanfang.AgainOrderRoomAct;
import com.xl.rentkeeper.act.kanfang.CancelOrderRoomAct;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.log.QLog;
import com.xl.rentkeeper.util.DialogUtil;
import com.xl.rentkeeper.util.TimeUtils;
import com.xl.rentkeeper.util.TipMapping;
import com.xl.rentkeeper.util.Util;
import com.xl.rentkeeper.view.TitleBar;

/**
 * Created by Administrator on 2015/8/25.
 */
public class ProcessOrderDetailsAct extends BaseAct implements View.OnClickListener {
    private TextView mTVName;
    private TextView mTVTime;
    private TextView mTVJob;
    private TextView mTVPrice;//金额
    private TextView mTVCheckin;//入住时间
    private TextView mTVRoomName1;//房间信息
    private TextView mTVRoomName2;//房间信息
    private TextView mTVRoominfoStatus1;
    private TextView mTVRoominfoStatus2;
    private TextView mTVRoominfoPrice1;
    private TextView mTVRoominfoPrice2;
    private TextView mTVRemark;//房间价格

    private LinearLayout mLLRoomInfo1;
    private LinearLayout mLLRoomInfo2;
    private View mLine;

    private TextView mTVCall;
    private Order mOrder;
    private Dialog mDialog;
    private LinearLayout mLLAction;
    private Button mBtnReceiver;
    private Button mBTAffirm;
    private Button mBTCancel;
    private Button mBTAgain;
    private TitleBar tb;
    private int mState = 0;
    private String reqContext ="ProcessOrderDetailsAct";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_order_details);
        tb = (TitleBar) findViewById(R.id.tb);
        setTranslucentStatus();
        initTitleBar(tb);
        initView();
        initData();
    }


    private void initView() {
        mTVName = (TextView) findViewById(R.id.tv_name);
        mTVTime = (TextView) findViewById(R.id.tv_orderTime);
        mTVJob = (TextView) findViewById(R.id.tv_job);
        mTVPrice = (TextView) findViewById(R.id.tv_price);
        mTVCheckin = (TextView) findViewById(R.id.tv_checkInDay);
        mTVRoomName1 = (TextView) findViewById(R.id.tv_roomName1);
        mTVRoomName2 = (TextView) findViewById(R.id.tv_roomName2);
        mTVRoominfoStatus1 = (TextView) findViewById(R.id.tv_roominfo_status1);
        mTVRoominfoStatus2 = (TextView) findViewById(R.id.tv_roominfo_status2);
        mTVRoominfoPrice1 = (TextView) findViewById(R.id.tv_roomPrice1);
        mTVRoominfoPrice2 = (TextView) findViewById(R.id.tv_roomPrice2);
        mTVRemark = (TextView) findViewById(R.id.tv_remark);
        mBtnReceiver = (Button) findViewById(R.id.btn_receiver);

        mLLRoomInfo1 = (LinearLayout) findViewById(R.id.ll_roomInfo1);
        mLLRoomInfo2 = (LinearLayout) findViewById(R.id.ll_roomInfo2);
        mLine = findViewById(R.id.view_line);

        mLLAction = (LinearLayout) findViewById(R.id.ll_action);
        mBTAffirm = (Button) findViewById(R.id.bt_affirm);
        mBTCancel = (Button) findViewById(R.id.bt_cancel);
        mBTAgain = (Button) findViewById(R.id.bt_again);
        mBTAffirm.setOnClickListener(this);
        mTVCall = (TextView) findViewById(R.id.tv_phone);
        mTVCall.setOnClickListener(this);
        mBTCancel.setOnClickListener(this);
        mBTAgain.setOnClickListener(this);
    }

    private void initData() {
        Intent intent = this.getIntent();
        mOrder = (Order) intent.getSerializableExtra("msg");

        mTVName.setText(mOrder.viewOrder.name);
        mTVTime.setText(Util.replaceTimeNull(mOrder.viewOrder.viewTime));
        mTVJob.setText(Util.replaceNull(null, mOrder.viewOrder.job));

        Util.setExpectPrice(mTVPrice,mOrder.viewOrder,"process_order_detail");

        mTVCheckin.setText(Util.replaceNull(null,mOrder.viewOrder.checkInDays));

        if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_NOT_RECEIVE.getValue()) {
            mBtnReceiver.setVisibility(View.VISIBLE);
            mLLAction.setVisibility(View.GONE);
            tb.setTitleName("接单详情");
        } else if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
            mBtnReceiver.setVisibility(View.GONE);
            mLLAction.setVisibility(View.VISIBLE);
            mBTAgain.setVisibility(View.GONE);
            tb.setTitleName("接单详情");
            mBTAffirm.setText("用户确认看房");
            mBTCancel.setText("用户放弃看房");
            mBTAgain.setText("用户确认看房");
        } else if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
            mBtnReceiver.setVisibility(View.GONE);
            mLLAction.setVisibility(View.VISIBLE);
            mBTAgain.setVisibility(View.VISIBLE);
            tb.setTitleName("看房详情");
            mBTAffirm.setText("用户确认订房");
            mBTCancel.setText("用户放弃订房");
            mBTAgain.setText("用户重新预约看房");
        }
        if (mOrder.viewOrder.viewRooms == null || mOrder.viewOrder.viewRooms.size() == 0) {
            mLLRoomInfo2.setVisibility(View.GONE);
            mLLRoomInfo1.setVisibility(View.GONE);
            mLine.setVisibility(View.GONE);
        } else {
            if (mOrder.viewOrder.viewRooms.size() == 1) {//只有一个房间
                mLLRoomInfo2.setVisibility(View.GONE);
                RoomInfo rInfo = mOrder.viewOrder.viewRooms.get(0);
                if (TextUtils.isEmpty(rInfo.name)) {
                    mTVRoomName1.setText(Util.replaceNull(null,rInfo.name));
                } else {
                    mTVRoomName1.setText(Util.replaceNull(null,rInfo.name));
                    Util.setRoomStatus(mTVRoominfoStatus1,rInfo,"order_detail");
                    Util.setRoomPrice(mTVRoominfoPrice1,rInfo,"order_detail");
                }
            } else {//两个房间
                RoomInfo rInfo = mOrder.viewOrder.viewRooms.get(0);
                if (TextUtils.isEmpty(rInfo.name)) {
                    mTVRoomName1.setText(Util.replaceNull(null,rInfo.name));
                } else {
                    mTVRoomName1.setText(Util.replaceNull(null,rInfo.name));
                    Util.setRoomStatus(mTVRoominfoStatus1,rInfo,"order_detail");
                    Util.setRoomPrice(mTVRoominfoPrice1,rInfo,"order_detail");
                }
                RoomInfo rInfo2 = mOrder.viewOrder.viewRooms.get(1);
                if (TextUtils.isEmpty(rInfo2.name)) {
                    mTVRoomName2.setText(Util.replaceNull(null,rInfo2.name));
                } else {
                    mTVRoomName2.setText(Util.replaceNull(null,rInfo2.name));
                    Util.setRoomStatus(mTVRoominfoStatus2,rInfo2,"order_detail");
                    Util.setRoomPrice(mTVRoominfoPrice2,rInfo2,"order_detail");
                }
            }
        }

            mTVRemark.setText(Util.replaceNull(null,mOrder.viewOrder.remark));
    }

    public void submit(View v) {
        mDialog = DialogUtil.showProgressDialog(this, R.string.loading);
        OrderLogic.getInstance().stewardOrder(mOrder.viewOrder.id,reqContext);//
    }

    public String[] monitorEvents() {
        return new String[]{PMS_LOGIC_StewardOrder};
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        if (event.equals(OrderLogic.PMS_LOGIC_StewardOrder)) {
            if(!reqContext.equals(data[2])){
                return;
            }
            if (mDialog != null && mDialog.isShowing()) {
                mDialog.dismiss();
            }
            if (ret) {
                finish();
            } else {
                App.showToast(errInfo);
            }
        }
    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        if (v == mBTAffirm) {
            if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
                mState = 1;
                intent = new Intent(this, AffirmOrderAct.class);
                intent.putExtra("msg", mOrder);
                startActivityForResult(intent, 110);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            } else if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                mState = 3;
                intent = new Intent(this, AffirmOrderRoomAct.class);
                intent.putExtra("msg", mOrder);
                startActivityForResult(intent, 110);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }
        } else if (v == mBTCancel) {
            if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {
                mState = 2;
                intent = new Intent(this, CancelOrderAct.class);
                intent.putExtra("id", mOrder.id);
                intent.putExtra("leadStewardId", mOrder.viewOrder.leadStewardId);
                startActivityForResult(intent, 110);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            } else if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                mState = 4;
                intent = new Intent(this, CancelOrderRoomAct.class);
                intent.putExtra("msg", mOrder);
                startActivityForResult(intent, 110);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }
        } else if (v == mBTAgain) {
            if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_RECEIVE.getValue()) {

            } else if (mOrder.viewOrder.status == ViewOrderStatus.ORDER_IS_VIEW.getValue()) {
                intent = new Intent(this, AgainOrderRoomAct.class);
                intent.putExtra("msg", mOrder);
                startActivityForResult(intent, 110);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }
        } else if (v == mTVCall) {
            Uri uri = Uri.parse("tel:" + mOrder.viewOrder.phone);
            intent = new Intent(Intent.ACTION_DIAL, uri);
            startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 110) {
            if (mState ==1) {//确认预约
                QLog.d(this,"mState "+mState);
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra("page",1);
                startActivity(intent);
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            } else if (mState ==2) {//取消预约
              /*  Intent intent = new Intent(this, NotViewOrderActivity.class);
                startActivity(intent);*/
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }else if (mState ==3) {//确认订房
                /*Intent intent = new Intent(this, SignOrderActivity.class);
                startActivity(intent);*/
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }else if (mState ==4) {//取消订房
               /* Intent intent = new Intent(this, NotSignOrderActivity.class);
                startActivity(intent);*/
                overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
            }
//            App.showToast(R.string.submit_succeed);
            finish();
            // overridePendingTransition(R.anim.base_slide_remain, R.anim.base_slide_right_out);
        }
    }
}
